package special;

class MinStack {
    private int min = Integer.MAX_VALUE;
    private Node top;
    public static class Node{
        int val;
        Node next;
    }
    public MinStack() {

    }
    
    public void push(int val) {
        min = Math.min(min, val);
        Node newT = new Node();
        newT.val = val;
        newT.next = top;
        top = newT;
    }
    
    public void pop() {
        if (top.val == min){
            int minn = Integer.MAX_VALUE;
            Node cur = top.next;
            while (cur != null){
                minn = Math.min(minn, cur.val);
                cur = cur.next;
            }
            this.min = minn;
        }
        top = top.next;
    }
    
    public int top() {
        return top.val;
    }
    
    public int getMin() {
        return min;
    }
}